﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Business.IManager;
using Business.Manager;
using Utility;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace OnlinePayment.OP
{
    public partial class PaymentHistoryView : System.Web.UI.Page
    {
        IOrderManager orderManager = new OrderManager();
        DataSet tuitionSet = null;
        DataSet tuitionDetailSet = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setAllFieldsetVisibleMode(false);
            }
        }



        protected void ddlPaymentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = ddlPaymentType.SelectedItem.Text.ToString();


            switch(text){
                case "请选择":
                    setAllFieldsetVisibleMode(false);
                    break;
                case "学费":
                    tuitionSet = orderManager.getTuitionOrderSet();
                    DL_Tuition.DataSource = tuitionSet;
                    DL_Tuition.DataBind();
                    setAllFieldsetVisibleMode(false);
                    fs_tuition.Visible = true;
                    break;
                case "双专业费":
                    setAllFieldsetVisibleMode(false);
                    fs_doublemajor.Visible = true;
                    break;
                case "水电费":
                    setAllFieldsetVisibleMode(false);
                    fs_utilities.Visible = true;
                    break;
                case "校园网费":
                    setAllFieldsetVisibleMode(false);
                    fs_campusnetwork.Visible = true;
                    break;
                case "学生卡充值":
                    Student student = (Student)Session["User"];
                    GV_StuCardBill.DataSource = orderManager.getStuCardOrderSetByStudentId(student.StudentId);
                    GV_StuCardBill.DataBind();
                    setAllFieldsetVisibleMode(false);
                    fs_studentcard.Visible = true;
                    break;
                case "显示全部":
                    setAllFieldsetVisibleMode(true);
                    break;
                default:
                    break;
            }
        }

        private void setAllFieldsetVisibleMode(bool mode)
        {
            fs_tuition.Visible = mode;
            fs_doublemajor.Visible = mode;
            fs_utilities.Visible = mode;
            fs_campusnetwork.Visible = mode;
            fs_studentcard.Visible = mode;
        }

        protected void DL_Tuition_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "CheckDetail": //查看订单详细信息
                    GridView gv = (GridView)e.Item.FindControl("GV_TuitionDetail");
                    //ClientScript.RegisterStartupScript(this.GetType(), "al", "alert('ssssss!')", true);
                    DataSet ds = orderManager.getTuitionDetailSet(e.CommandArgument.ToString());
                    gv.DataSource = ds;
                    gv.DataBind();
                    break;
            }
        }


        protected void btnPrintOrderReports_Click(object sender, EventArgs e)
        {
            Document myDocument = new Document(PageSize.A4, 50, 50, 50, 50);
            try
            {
                PdfWriter.GetInstance(myDocument, new FileStream(Server.MapPath("../Output/Tuition.pdf"), FileMode.Create));
                myDocument.Open();
                string fontPath = Environment.GetEnvironmentVariable("WINDIR") + "\\FONTS\\SIMHEI.TTF";//强制中文字体，否则无法显示中文
                BaseFont baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

                  
                //标题居中
                Paragraph paragraph = new Paragraph("缴交学费", new Font(baseFont, 16));
                paragraph.Alignment = Element.ALIGN_CENTER;
                myDocument.Add(paragraph);

                List list = new iTextSharp.text.List(true, 20);
                tuitionSet = orderManager.getTuitionOrderSet();
                if (tuitionSet != null)
                {
                   
                    DataRowCollection rowc = tuitionSet.Tables[0].Rows;
                    for (int i = 0; i < rowc.Count; i++)
                    {
                        list.Add(new iTextSharp.text.ListItem("\n 订单编号 : " + rowc[i]["orderId"]
                            + "\n 缴费日期 : " + rowc[i]["orderDate"]
                            + "\n 订单金额 : " + rowc[i]["totalAmount"]
                            + "\n 缴费状态 : " + rowc[i]["orderStatus"]
                            + "\n"
                            , new Font(baseFont, 10)));
                    }
                    myDocument.Add(list);
                }

                myDocument.Close();

                try  //测试用
                {
                    System.Diagnostics.Process.Start(Server.MapPath("../Output/Tuition.pdf"));
                }
                catch (Exception ex)
                {

                }


            }
            catch (Exception ex)
            {
            }
        }

        protected void DL_Tuition_ItemDataBound(object sender, DataListItemEventArgs e)
        {

            /*string orderId = sender
                //this.DL_Tuition.DataKeys[e.Item.ItemIndex].ToString();
            GridView gv = (GridView)e.Item.FindControl("GV_TuitionDetail");
            gv.DataSource = orderManager.getTuitionDetailSet(Convert.ToInt32(orderId));
            gv.DataBind();*/
        }

    }
}